#include "treeitem.h"


//TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
TreeItem::TreeItem(const QString &data, TreeItem *parent)
{
  m_parentItem = parent;
  m_itemData = data;
}

//A pointer to each of the child items belonging to this item will be stored in the childItems private member variable. When the class's destructor is called, it must delete each of these to ensure that their memory is reused:

TreeItem::~TreeItem()
{
  qDeleteAll(m_childItems);
}

//Since each of the child items are constructed when the model is initially populated with data, the function to add child items is straightforward:

void TreeItem::appendChild(TreeItem *item)
{
  m_childItems.append(item);
}

//Each item is able to return any of its child items when given a suitable row number. For example, in the above diagram, the item marked with the letter "A" corresponds to the child of the root item with row = 0, the "B" item is a child of the "A" item with row = 1, and the "C" item is a child of the root item with row = 1.
//The child() function returns the child that corresponds to the specified row number in the item's list of child items:

TreeItem *TreeItem::child(int row)
{
  return m_childItems.value(row);
}

//The number of child items held can be found with childCount():

int TreeItem::childCount() const
{
  return m_childItems.count();
}

//The TreeModel uses this function to determine the number of rows that exist for a given parent item.
//The row() function reports the item's location within its parent's list of items:

int TreeItem::row() const
{
  if (m_parentItem)
      return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));

  return 0;
}

//Note that, although the root item (with no parent item) is automatically assigned a row number of 0, this information is never used by the model.
//The number of columns of data in the item is trivially returned by the columnCount() function.

//int TreeItem::columnCount() const
//{
//  return m_itemData.count();
//}

//Column data is returned by the data() function, taking advantage of QList's ability to provide sensible default values if the column number is out of range:

//QVariant TreeItem::data(int column) const
//{
//  return m_itemData.value(column);
//}

QString TreeItem::data() const
{
    return m_itemData;
}

void TreeItem::setData(const QString &data)
{
    m_itemData = data;
}

//The item's parent is found with parent():

TreeItem *TreeItem::parentItem()
{
  return m_parentItem;
}
